﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data;
using ExtBusiness;
using ExtBusiness.Admin;
using WF_Business;

/// <summary>
/// Monitor 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Monitor : System.Web.Services.WebService
{
    public Monitor()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }
 
    /// <summary>
    /// 获取所有未结束状态的案例。
	/// 未结束的依据是status<2。
    /// </summary>
    /// <returns></returns>
    [WebMethod(true)]
    public DataSet getNoEndedInstances()
    {
        DataSet rtn;
        string userid = Session["UserId"].ToString();

        bool isSystemUser = SystemManager.IsSystemUser(userid);//监察室人员与办公室人员
      
        string sql = string.Empty;
        if (isSystemUser)//监察室与管理员    //添加业务删除标识and b.isdelete <>1  
        {
            sql = string.Format(@"select b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.STEP,b.exusedtime,b.extotaltime,b.exendtime,b.exalarmtime,b.PRIORITY 
                     from  st_work_item a,st_instance b,ST_WORKFLOW c
                     where (a.active=0 or a.active=1) and b.status<2 
                     and a.iid=b.iid and b.wid=c.wid  and b.isdelete <>1 
                     group by b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.step,b.exusedtime,b.extotaltime,b.exendtime,b.exalarmtime,b.PRIORITY
                     order by a.iid desc");
        }
        else
        {
            sql = string.Format(@"select b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.STEP,b.exusedtime,b.extotaltime,b.exendtime,b.exalarmtime,b.PRIORITY 
                     from  st_work_item a,st_instance b,ST_WORKFLOW c
                     where (a.active=0 or a.active=1) and b.status<2 
                     and c.wname in(select task from xt_query_right where userid='{0}')
                     and a.iid=b.iid and b.wid=c.wid and b.isdelete <>1 
                     group by b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.step,b.exusedtime,b.extotaltime,b.exendtime,b.exalarmtime,b.PRIORITY
                     order by a.iid desc", userid);
        }
        SysParams.OAConnection().RunSql(sql, out rtn);
        return rtn;
    }

    /// <summary>
    /// 获取所有非过期的案例的岗位执行情况。
	/// * exusedtime 已经执行的时间
	/// * extotaltime 总时间
	/// * exalarmtime 执行时间
	/// ST_WORKFLOW 获取处罚种类
    /// </summary>
    /// <returns></returns>
    [WebMethod(true)]
    public DataSet getWorkInstances()
    {
        DataSet rtn = new DataSet();
        string userid = Session["UserId"].ToString();

        bool isSystemUser = SystemManager.IsSystemUser(userid);//监察室人员与办公室人员
       
        string sql = string.Empty;
        if (isSystemUser)//修改为:监察室与管理员  //添加业务删除标识and b.isdelete <>1  
        {
            sql = string.Format(@"select b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.STEP,a.exusedtime,a.extotaltime,a.exendtime,a.exalarmtime,b.PRIORITY 
                     from  st_work_item a,st_instance b,ST_WORKFLOW c
                     where (a.active=0 or a.active=1) and b.status<2 
                     and a.iid=b.iid and b.wid=c.wid  and b.isdelete <>1 
                     group by b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.STEP,a.exusedtime,a.extotaltime,a.exendtime,a.exalarmtime,b.PRIORITY
                     order by a.iid desc");
        }
        else
        {
            sql = string.Format(@"select b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.STEP,a.exusedtime,a.extotaltime,a.exendtime,a.exalarmtime,b.PRIORITY 
                     from  st_work_item a,st_instance b,ST_WORKFLOW c
                     where (a.active=0 or a.active=1) and b.status<2 
                     and c.wname in(select task from xt_query_right where userid='{0}')
                     and a.iid=b.iid and b.wid=c.wid  and b.isdelete <>1 
                     group by b.status,b.name,c.WNAME,b.ACCEPTED_TIME,a.iid,a.STEP,a.exusedtime,a.extotaltime,a.exendtime,a.exalarmtime,b.PRIORITY
                     order by a.iid desc", userid);
        }
        SysParams.OAConnection().RunSql(sql, out rtn);
        return rtn;
    }

    /// <summary>
    /// 根据iid获取制定案件。
    /// </summary>
    /// <param name="iid"></param>
    /// <returns></returns>
    [WebMethod]
    public DataSet getInstanceById(String iid)
    {
        DataSet rtn = new DataSet();
        String sql = "select * from st_instance where iid=" + iid;
        SysParams.OAConnection().RunSql(sql, out rtn);
        return rtn;
    }

    /// <summary>
    /// 根据iid获取指定案件岗位情况。
	/// 其实可以返回多条，但页面上只需要一条。
    /// </summary>
    /// <param name="iid"></param>
    /// <returns></returns>
    [WebMethod]
    public DataSet getWorkInstanceById(String iid)
    {
        DataSet rtn = new DataSet();
        String sql = "select * from st_work_item where iid=" + iid + " and rownum=1";
        SysParams.OAConnection().RunSql(sql, out rtn);
        return rtn;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="iids">以","分隔</param>
    /// <returns></returns>
    [WebMethod]
    public DataSet getUsersByInstanceIds(String iids)
    {
        DataSet rtn = new DataSet();     
        String sql = "select w.IID,u.USER_NAME from st_work_item w" +
                " left join st_user u on u.userid=w.userid " +
                "where w.iid in (" + iids + ") order by w.iid desc";//要和上面查询案例的顺序一样。这是出于页面上效率的考虑      
        SysParams.OAConnection().RunSql(sql, out rtn);
        return rtn;
    }
}

